#include <stdio.h>
#include <string.h>

//字体：新宋体
//取模方式： 阴码，逐列式，顺向

//使用PCtoLCD2002完美版生成字库.
//字库中不存在的填充为空格.
int main(int argc, char **argv)
{
    FILE *fp;
    fp = fopen("GB2312.txt", "w");
    if (fp == NULL)
    {
        printf("open file faild\r\n");
        return -1;
    }

    char GB2312[87 * 2 * 6 * 16];
    int per_area = 6 * 16;

    for (int i = 1; i <= 87; i++)
    {
        for (int j = 0; j < per_area; j++)
        {
            if (i >= 10 && i <= 15)
            {
                GB2312[((i - 1) * per_area + j) * 2] = 0x20;
                GB2312[((i - 1) * per_area + j) * 2 + 1] = 0x20;
            }
            else
            {
                GB2312[((i - 1) * per_area + j) * 2] = 0xa0 + i;
                GB2312[((i - 1) * per_area + j) * 2 + 1] = 0xa0 + j;
            }
        }

        GB2312[((i - 1) * per_area + 0) * 2] = 0x20;
        GB2312[((i - 1) * per_area + 0) * 2 + 1] = 0x20;

        GB2312[((i - 1) * per_area + (per_area - 1)) * 2] = 0x20;
        GB2312[((i - 1) * per_area + (per_area - 1)) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////
    //55区最后没有编码。
    {
        GB2312[((55 - 1) * per_area + (per_area - 2)) * 2] = 0x20;
        GB2312[((55 - 1) * per_area + (per_area - 2)) * 2 + 1] = 0x20;

        GB2312[((55 - 1) * per_area + (per_area - 3)) * 2] = 0x20;
        GB2312[((55 - 1) * per_area + (per_area - 3)) * 2 + 1] = 0x20;

        GB2312[((55 - 1) * per_area + (per_area - 4)) * 2] = 0x20;
        GB2312[((55 - 1) * per_area + (per_area - 4)) * 2 + 1] = 0x20;

        GB2312[((55 - 1) * per_area + (per_area - 5)) * 2] = 0x20;
        GB2312[((55 - 1) * per_area + (per_area - 5)) * 2 + 1] = 0x20;

        GB2312[((55 - 1) * per_area + (per_area - 6)) * 2] = 0x20;
        GB2312[((55 - 1) * per_area + (per_area - 6)) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////
    //1区未编码部分
    GB2312[((1 - 1) * per_area + 1) * 2] = 0x20;
    GB2312[((1 - 1) * per_area + 1) * 2 + 1] = 0x20;
    ///////////////////////////////////////////////////////////////////////////
    //2区未编码部分
    for (int i = 0; i <= 16; i++)
    {
        GB2312[((2 - 1) * per_area + i) * 2] = 0x20;
        GB2312[((2 - 1) * per_area + i) * 2 + 1] = 0x20;
    }

    GB2312[((2 - 1) * per_area + 4 * 16 + 3) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 4 * 16 + 3) * 2 + 1] = 0x20;

    GB2312[((2 - 1) * per_area + 4 * 16 + 4) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 4 * 16 + 4) * 2 + 1] = 0x20;

    GB2312[((2 - 1) * per_area + 4 * 16 + 0xf) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 4 * 16 + 0xf) * 2 + 1] = 0x20;

    GB2312[((2 - 1) * per_area + 5 * 16 + 0) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 5 * 16 + 0) * 2 + 1] = 0x20;

    GB2312[((2 - 1) * per_area + 5 * 16 + 0xd) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 5 * 16 + 0xd) * 2 + 1] = 0x20;

    GB2312[((2 - 1) * per_area + 5 * 16 + 0xe) * 2] = 0x20;
    GB2312[((2 - 1) * per_area + 5 * 16 + 0xe) * 2 + 1] = 0x20;
    ///////////////////////////////////////////////////////////////////////////
    //4区未编码部分
    for (int i = 4; i <= 0xf; i++)
    {
        GB2312[((4 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((4 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////
    //5区未编码部分
    for (int i = 7; i <= 0xf; i++)
    {
        GB2312[((5 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((5 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////
    //6区未编码部分
    for (int i = 9; i <= (0xf + 1); i++)
    {
        GB2312[((6 - 1) * per_area + 1 * 16 + i) * 2] = 0x20;
        GB2312[((6 - 1) * per_area + 1 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 9; i <= (0xf); i++)
    {
        GB2312[((6 - 1) * per_area + 3 * 16 + i) * 2] = 0x20;
        GB2312[((6 - 1) * per_area + 3 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0; i <= (0xf); i++)
    {
        GB2312[((6 - 1) * per_area + 4 * 16 + i) * 2] = 0x20;
        GB2312[((6 - 1) * per_area + 4 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0; i <= (0xf); i++)
    {
        GB2312[((6 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((6 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////
    //7区未编码部分
    for (int i = 2; i <= (0xf + 1); i++)
    {
        GB2312[((7 - 1) * per_area + 2 * 16 + i) * 2] = 0x20;
        GB2312[((7 - 1) * per_area + 2 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 2; i <= (0xf); i++)
    {
        GB2312[((7 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((7 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////

    //8区未编码部分
    for (int i = 0xb; i <= 0xf; i++)
    {
        GB2312[((8 - 1) * per_area + 1 * 16 + i) * 2] = 0x20;
        GB2312[((8 - 1) * per_area + 1 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0; i <= 4; i++)
    {
        GB2312[((8 - 1) * per_area + 2 * 16 + i) * 2] = 0x20;
        GB2312[((8 - 1) * per_area + 2 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0xa; i <= (0xf); i++)
    {
        GB2312[((8 - 1) * per_area + 4 * 16 + i) * 2] = 0x20;
        GB2312[((8 - 1) * per_area + 4 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0; i <= (0xf); i++)
    {
        GB2312[((8 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((8 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }

    ///////////////////////////////////////////////////////////////////////////
    //9区未编码部分
    for (int i = 0; i <= 3; i++)
    {
        GB2312[((9 - 1) * per_area + 0 * 16 + i) * 2] = 0x20;
        GB2312[((9 - 1) * per_area + 0 * 16 + i) * 2 + 1] = 0x20;
    }

    for (int i = 0; i <= (0xf); i++)
    {
        GB2312[((9 - 1) * per_area + 5 * 16 + i) * 2] = 0x20;
        GB2312[((9 - 1) * per_area + 5 * 16 + i) * 2 + 1] = 0x20;
    }
    ///////////////////////////////////////////////////////////////////////////

    fwrite(GB2312, sizeof(GB2312), 1, fp);

    fclose(fp);

    return 0;
}
